"""
Case Type   : ansi_nulls
Case Name   : gs_guc方式设置ansi_nulls参数
Create At   : 2025/6/11
Owner       : @zhanghuan96
Description :
    1、使用gs_guc set方式设置参数为ansi_nulls为on
    2、使用gs_guc reload方式设置参数ansi_nulls为off
    3、恢复默认值
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class PlpgsqlDependency(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.common = Common()
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.default_value = self.common.show_param('ansi_nulls')

    def test_guc(self):
        text = '--step1:使用gs_guc set方式设置参数ansi_nulls为on;expect:成功--'
        self.log.info(text)
        mod_msg = self.pri_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            "ansi_nulls =on")
        self.log.info(mod_msg)
        self.assertTrue(mod_msg)
        restart_msg = self.pri_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)
        sql_cmd = "show ansi_nulls;"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual('on',
                         sql_res.splitlines()[-2].strip(),
                         '查询失败')

        text = '--step2:使用gs_guc reload方式设置参数ansi_nulls为off;expect:成功--'
        self.log.info(text)
        mod_msg = self.pri_sh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            "ansi_nulls =off")
        self.log.info(mod_msg)
        self.assertTrue(mod_msg)
        sql_cmd = "show ansi_nulls;"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual('off',
                         sql_res.splitlines()[-2].strip(),
                         '查询失败')

    def tearDown(self):
        text = '----step3:恢复默认值;expect:成功----'
        self.log.info(text)
        mod_msg = self.pri_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f"ansi_nulls "
                                            f"='{self.default_value}'")
        self.log.info(mod_msg)
        self.assertTrue(mod_msg)
        restart_msg = self.pri_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
